package com.cheng.xbjy.service.impl;

import com.cheng.xbjy.dao.DeptDao;
import com.cheng.xbjy.dao.UserDao;
import com.cheng.xbjy.entity.Dept;
import com.cheng.xbjy.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptDao deptDao;

    @Autowired
    private UserDao userDao;

    @Override
    public List<Dept> findAll() {
        return deptDao.findAll();
    }

    @Override
    public List<Map<String, Object>> findAllAndUser() {
        List<Map<String, Object>> result = new ArrayList<>();
        List<Dept> deptList = deptDao.findAll();
        for (Dept dept : deptList) {
            Map<String, Object> map = new HashMap<>();
            map.put("deptName", dept.getName());
            //查询部门的人数
            map.put("countDeptUser", userDao.countDeptUser(dept.getId()));
            //查询部门的所有员工
            map.put("deptUsers", userDao.findDeptUser(dept.getId()));
            result.add(map);
        }
        return result;
    }
}
